package com.wlgzs.booksystem.controller.back;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlgzs.booksystem.entity.Book;
import com.wlgzs.booksystem.entity.User;
import com.wlgzs.booksystem.enums.Result;
import com.wlgzs.booksystem.enums.ResultCodeEnum;
import com.wlgzs.booksystem.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wlgzs-sjl
 * @since 2020-11-17
 */
@RestController
@RequestMapping("/back/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    UserService userservice;

    @GetMapping
    @ApiOperation(value = "后台用户管理主页", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页(默认为1)", defaultValue = "1", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每一页的大小(默认为10)", defaultValue = "5", required = true, dataType = "int")
    })
    public Result bookIndex(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) {
        Page<User> page = new Page<>(current, size);
        List<User> users = userservice.page(page).getRecords();
        List<Object> objects = new ArrayList<>();
        objects.add(users);
        long countPage = (page.getTotal() + size) / size;
        objects.add(countPage);
        return new Result(ResultCodeEnum.SUCCESS, objects);
    }

    @GetMapping("/updateIndex")
    @ApiOperation(value = "后台修改用户页面", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int")
    })
    public Result updateIndex(Integer userId) {
        User user = userservice.getById(userId);
        return new Result(ResultCodeEnum.SUCCESS, user);
    }

    @GetMapping("/findByName")
    @ApiOperation(value = "根据用户名查找用户", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String")
    })
    public Result findUsersByName(String name) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("user_name", name);
        List<User> users = userservice.list(wrapper);
        if (users != null) {
            return new Result(ResultCodeEnum.FIND, users);
        } else {
            return new Result(ResultCodeEnum.UNFIND);
        }
    }

    @DeleteMapping
    @ApiOperation(value = "删除用户", httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int")
    })
    public Result deleteUserById(int userId) {
        if (userservice.removeById(userId)) {
            return new Result(ResultCodeEnum.DELETE);
        } else {
            return new Result(ResultCodeEnum.UNDELETE);
        }
    }

    @DeleteMapping("/many")
    @ApiOperation(value = "批量删除用户", httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIds", value = "用户id的集合", required = true, dataType = "String")
    })
    public Result deleteManyUsersByIds(String userIds) {
        String[] list = userIds.split(",");
        for (String s : list) {
            userservice.removeById(Integer.parseInt(s));
        }
        return new Result(ResultCodeEnum.DELETE);
    }

    @PostMapping
    @ApiOperation(value = "添加或修改用户", httpMethod = "POST", response = Result.class)
    public Result insertUser(User user) {
        if (userservice.saveOrUpdate(user)) {
            return new Result(ResultCodeEnum.SUCCESS);
        } else {
            return new Result(ResultCodeEnum.FAIL);
        }
    }
}